package com.example.datn.transformer;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.example.datn.objects.AlbumObject;
import com.example.datn.objects.SongObject;
import com.example.datn.objects.VideoObject;

public class Transformer {

	static JSONObject jsonResponse;
	ArrayList<SongObject> listSong = null;
	ArrayList<AlbumObject> listAlbum = null;

	public Transformer() {
		listSong = new ArrayList<SongObject>();
		listAlbum = new ArrayList<AlbumObject>();
	}

	public void TransformToListSong(String json) {
		int length;
		JSONArray jsonMainNode;
		try {
			// gson.fromJson(json, SongObject.class);
			jsonResponse = new JSONObject(json);

			// -------------[ lay ve danh sach cac bai hat ]------------------
			jsonMainNode = jsonResponse.optJSONArray("songs");
			length = jsonMainNode.length();
			for (int i = 0; i < length; i++) {
				JSONObject jsonObSong = jsonMainNode.getJSONObject(i);

				SongObject song = new SongObject();
				song.songNo = Integer.parseInt(jsonObSong.getString("SongNo"));
				song.songName = jsonObSong.getString("SongName");
				//song.albumId = Integer.parseInt(jsonObSong.getString("AlbumId"));
				song.linkAudio = jsonObSong.getString("LinkSong");
				song.luotNghe = Integer.parseInt(jsonObSong.getString("SoLuotNghe"));
				song.luotThich = Integer.parseInt(jsonObSong.getString("SoLuotThich"));
//				song.setTotalSongTime(Integer.parseInt(jsonObSong
//						.getString("TongThoiGian")));

				listSong.add(song);
			}

			// ------------------[ lay ve danh sach cac album
			// ]-------------------
			jsonMainNode = jsonResponse.optJSONArray("albums");
			length = jsonMainNode.length();
			for (int i = 0; i < length; i++) {
				JSONObject jsonObSong = jsonMainNode.getJSONObject(i);

				AlbumObject album = new AlbumObject();
//				album.setAlbumId(Integer.parseInt(jsonObSong
//						.getString("AlbumId")));
				album.albumName = jsonObSong.getString("AlbumName");
				album.albumDetail = jsonObSong.getString("AlbumDetails");
				album.soLuotNghe = Integer.parseInt(jsonObSong.getString("SoLuotNghe"));
				album.linkImage = jsonObSong.getString("LinkImage");
				album.singerId = Integer.parseInt(jsonObSong.getString("Authors"));

				listAlbum.add(album);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static ArrayList<VideoObject> TransformToListVideo(String json){
		ArrayList<VideoObject> videos = new ArrayList<VideoObject>();
		int length;
		JSONArray jsonMainNode;
		VideoObject video;
		
		try{
			jsonResponse = new JSONObject(json);
			jsonMainNode = jsonResponse.optJSONArray("videos");
			length = jsonMainNode.length();
			for (int i = 0; i < length; i++) {
				JSONObject jsonObSong = jsonMainNode.getJSONObject(i);
				
				video = new VideoObject();
				video.idVideo = Integer.parseInt(jsonObSong.getString("id"));
				video.videoName = jsonObSong.getString("videoName");
				video.videoId = jsonObSong.getString("videoId");
				video.singerId = Integer.parseInt(jsonObSong.getString("singerID"));
				
				videos.add(video);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return videos;
	}
	
	public ArrayList<SongObject> getListSong() {
		return listSong;
	}

	public ArrayList<AlbumObject> getListAlbum() {
		return listAlbum;
	}

}
